package controller;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import po.User;
import service.UserService;
/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;

	
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String username,String password, Model model, 
                                                          HttpSession session) {
		// 通过账号和密码查询用户
		User user = userService.findUser(username, password);
		if(user != null){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
			return "redirect:user/getUsers.action";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	

	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "login";
	}
//	@RequestMapping(value = "/user/userlist.action", method = RequestMethod.GET)
//	public String userlist(Model model) {
//		List list=userService.getUserlist();
//		model.addAttribute("userlist", list);	
//	    return "user_list1";
//	}	
	/**
	 *  用户列表
	 */
	
	@RequestMapping(value = "/user/getUsers.action")
	public String list( Model model) {
		
		List<User> userlist = userService.getUserList();
		//System.out.println(userlist.size());
		model.addAttribute("userlist", userlist);	
		return "user_list";
	}
	/**
	 * 删除用户信息
	 */
	@RequestMapping("/user/delUser.action")
	public String userDelete(Integer userid) {
		int rows = userService.deleteUser(userid);
		if(rows > 0) {
			System.out.println("OK");
		}else {
			System.out.println("FAIL");
		}
		//System.out.println(userid);
		return "redirect:/user/getUsers.action";
	}
	/**
	 * 添加用户信息
	 */
	@RequestMapping("/user/addUser.action")
	public String userAdd(User user) {
		int rows = userService.addUser(user);
		if(rows > 0) {
			System.out.println("OK");
		}else {
			System.out.println("FAIL");
		}
		return "redirect:/user/getUsers.action";
	}
	@RequestMapping(value ="/user/getUserId.action")
	@ResponseBody
	public User getUserId(Integer user_id) {
		User data = userService.getUserId(user_id);
		return data;
   }  
	
	@RequestMapping(value ="/user/updateUser.action")
	@ResponseBody
	public String updateUser(User user) {
		int row = userService.updateUser(user);
		System.out.println("row"+user.getUser_name());
		if(row>0) {
			return "OK";
		}else {
			return "false";
		}
   }
	
	@RequestMapping(value = "/user/findAll.action")
	public String listAll(String data,Model model) {	
		List<User> userlist = userService.findList(data);
		model.addAttribute("userlist", userlist);	
		return "user_list";
	}
}
